//
//  YUIViewController+Tips.swift
//  EasyEverything
//
//  Created by YangZhe on 17/1/23.
//  Copyright © 2017年 kuletech. All rights reserved.
//
extension YUIViewController{    
    open func yPrint(_ message: Any){
        if YConfig.DEBUG { print("\(YConfig.YLOGTAG)：\(message)") }
    }
    
    open func yToast(_ message: String) {
        if(TextUtils.isEmpty(message)){
            return
        }
        yCloseKeyBoard()
        yRunOnUIThread {
            JRToast.show(withText: message, bottomOffset: 100)
        }
    }
    
    open func yShowLoadingProgress(){
        yCloseKeyBoard()
        yRunOnUIThread {
            UIApplication.shared.isNetworkActivityIndicatorVisible = true
            YProgressDialog.showProgress(viewController: self)
        }
    }
    
    open func yDismissLoadingProgress(){
        yCloseKeyBoard()
        yRunOnUIThread {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            YProgressDialog.dismissProgress(viewController: self)
        }
    }
    
    open func yShowLoadingProgress2(){
        yCloseKeyBoard()
        yRunOnUIThread {
            UIApplication.shared.isNetworkActivityIndicatorVisible = true
            YProgressDialog.showProgress2(viewController: self)
        }
    }
    
    open func yDismissLoadingProgress2(){
        yCloseKeyBoard()
        yRunOnUIThread {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            YProgressDialog.dismissProgress2(viewController: self)
        }
    }
    
    open func ySendBroadcast(_ action:String,_ userInfo:[AnyHashable : Any]? = nil){
        yRunOnUIThread {
            //NotificationCenter.default.post(name: NSNotification.Name(rawValue: "\(action)"), object: object)
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "\(action)"), object: nil, userInfo:userInfo)
        }
    }
    
    open func yRegisterReceiver(_ action:String,receiver:@escaping (Notification) -> Void){
        if notifications.contains(action){
            return
        }
        notifications.append(action)
        nsObjectProtocols.append(NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "\(action)"), object: nil, queue: nil, using: receiver))
    }
    
    open func yUnRegisterReceiver(_ action:String) {
        var i = 0
        notifications.forEach {
            if $0 == action {
                notifications.remove(at: i)
                NotificationCenter.default.removeObserver(nsObjectProtocols[i])
                nsObjectProtocols.remove(at: i)
                return
            }
            i += 1
        }
    }
}
